package com.zust513.mapper;

import com.zust513.entity.Account;
import com.zust513.entity.Order;
import com.zust513.entity.Transaction;
import com.zust513.entity.User;
import org.apache.ibatis.annotations.*;

import java.math.BigDecimal;
import java.util.List;

@Mapper
public interface ClientMapper {

    //个人信息+密码查询修改
    @Select("SELECT * FROM accounts WHERE user_id=#{id}")
    Account selectAccountById(Integer id);
    @Update("update users set username=#{username},email=#{email},phone=#{phone} WHERE user_id=#{userId}")
    @Options(useGeneratedKeys = true, keyProperty = "user_id")
    int updateUserInfo(User user);
    @Select("SELECT * FROM users WHERE user_id=#{id}")
    User selectUserById(Integer id);
    @Update("update users set password_hash=#{password} WHERE user_id=#{userId}")
    int updatePassword(String password,Integer userId);
    //订单相关
    @Insert("INSERT INTO orders(user_id,address,total_amount)" +
            "VALUES(#{userId},#{address},#{totalAmount})")
    void insertOrder(Order order);
    @Update("update accounts set total_expense=#{totalExpense},balance=#{balance} WHERE user_id=#{userId}")
    void updateAccountPay(@Param("userId")Integer userId, @Param("balance")BigDecimal balance, @Param("totalExpense")BigDecimal totalExpense);
    @Select("SELECT * FROM orders WHERE user_id = #{userId} ORDER BY created_at DESC LIMIT 1")
    Order selectLatestOrderByUserId(Integer userId);
    //充值
    @Update("update accounts set total_recharge=#{totalRecharge},balance=${balance} WHERE user_id=#{userId}")
    void updateAccountRecharge(@Param("userId") Integer userId,
                               @Param("totalRecharge") BigDecimal totalRecharge,@Param("balance") BigDecimal balance);
    //交易记录
    @Insert("INSERT INTO transactions(account_id,type,amount,order_id)" +
            "VALUES(#{accountId},#{type},#{amount},#{orderId})")
    void insertTransaction(Transaction transaction);

    @Insert("INSERT INTO transactions(account_id,type,amount)" +
            "VALUES(#{accountId},#{type},#{amount})")
    void insertTransactionRecharge(Transaction transaction);

    @Select("SELECT SUM(amount) FROM transactions WHERE account_id = #{accountId} AND type = 'expense'")
    BigDecimal sumExpenseAmountByAccountId(@Param("accountId") Integer accountId);

    @Select("SELECT COUNT(*) FROM transactions WHERE account_id = #{accountId} AND type = 'expense'")
    Integer countExpenseRecordsByAccountId(@Param("accountId") Integer accountId);

    @Select("SELECT * FROM transactions WHERE account_id = #{accountId}")
    List<Transaction> getTransactionsByAccountId(@Param("accountId") Integer accountId);

    //订单列表
    @Select("SELECT * FROM orders WHERE user_id = #{userId}")
    List<Order> getOrdersByUserId(@Param("userId") Integer userId);

    @Select("SELECT * FROM orders WHERE order_id = #{orderId}")
    Order selectOrderByOrderId(@Param("orderId") Integer orderId);

    @Update("UPDATE orders SET delivery_status = 'completed' WHERE order_id = #{orderId}")
    void updateDeliveryStatusToCompleted(Integer orderId);
}
